package com.cskaoyan.user.service.impl;

import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.converter.UserConverterMapper;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.entitys.MemberInfo;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.form.UserLoginRequest;
import com.cskaoyan.user.service.ILoginService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Description：
 * @Author： 黄武
 * @Date： 2022/12/3 18:05
 */
@Service
public class LoginServiceImpl implements ILoginService {

    @Autowired
    MemberMapper memberMapper;

    @Autowired
    UserConverterMapper userConverterMapper;

    /**
     *  用户登陆
     * @param request
     * @return
     */
    @Override
    public UserLoginResponse login(UserLoginRequest request) {
        // 判空
        if (StringUtils.isEmpty(request.getUserName()) || StringUtils.isEmpty(request.getUserPwd())){
            throw new ValidateException(UserRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode(),
                    UserRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        // 因为注册的时候，密码用了md5处理，所以登录的时候也需要把密码经过md5处理之后再和数据库进行对比
        String pwd = DigestUtils.md5DigestAsHex(request.getUserPwd().getBytes());
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username", request.getUserName()).
                andEqualTo("password",pwd);
        List<Member> members = memberMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(members)){
            throw new ValidateException(UserRetCode.USERORPASSWORD_ERRROR.getCode(),
                    UserRetCode.USERORPASSWORD_ERRROR.getMessage());
        }
        // 如果查询的结果不为空，又因为username不能重复，那么结果就只有一个
        // 取出这个结果，如果尚未激活，也是不能登陆
        if ("N".equals(members.get(0).getIsVerified())){
            throw new ValidateException(UserRetCode.USER_ISVERFIED_ERROR.getCode(),
                    UserRetCode.USER_ISVERFIED_ERROR.getMessage());
        }
        // 账户存在且已激活，可以登陆
        UserLoginResponse userLoginResponse = userConverterMapper.converter(members.get(0));
        return userLoginResponse;
    }


    /**
     *  验证用户登陆
     * @param memberInfo
     */
    @Override
    public void verifyLogin(MemberInfo memberInfo) {
        long id = memberInfo.getUid();
        String username = memberInfo.getUsername();
        // 判空
        if(id == 0 || StringUtils.isEmpty(username)){
            throw new ValidateException(UserRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode(),
                    UserRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        // 查找是否存在这样一个账号
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("id", id).
                andEqualTo("username", username).
                andEqualTo("isVerified","Y");
        List<Member> members = memberMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(members)){
            throw new ValidateException(UserRetCode.USER_INFOR_INVALID.getCode(),
                    UserRetCode.USER_INFOR_INVALID.getMessage());
        }
    }
}
